var app = angular.module('webApp', ['ui.router', 'oc.lazyLoad', 'ngCookies','iService','iDirective','Filter','ngAnimate','ngSanitize','ui.bootstrap'/*,'ksSwiper'*//*,'angularFileUpload'*/]);
// 页面路由
app.config(['$controllerProvider','$stateProvider','$urlRouterProvider', '$locationProvider', '$ocLazyLoadProvider',function($controllerProvider,$stateProvider,$urlRouterProvider,$locationProvider,$ocLazyLoadProvider) {
    app.register = {
        controller: $controllerProvider.register
    };
    $ocLazyLoadProvider.config({
        debug: false
    });
    //路由状态重写
    var loadResolve = function (files,options) {
        var loadPlugin = {
            deps: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                    files: files
                });
            }]
        }
        if(options){
            angular.forEach(options,function (value,key) {
                loadPlugin[key] = value;
            })
        }
        return loadPlugin;
    }
    //动态路由
    $stateProvider
    .state('home', {    //首页
        url:'/',
        templateUrl: 'views/home.html',
        controller:'homeCtrl',
        resolve : loadResolve([__uri('../css/home.less')])
    })
    $urlRouterProvider.otherwise('/');
    // $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('');
}]);


// 在所有route跳转前进行登录检查，目标页面需要登录且用户未登录，则跳转到登录页面
app.run(['$rootScope', 'Context', '$location', function($rootScope,Context,$location) {
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
        /*if(toState.name=='enrol' && !Context.isUserLogin()){
            $location.path('/login');
        }*/
    })
}]);


//公用的方法
app.controller('appCtrl',['$scope','$rootScope','$location','$http','$timeout', function ($scope,$rootScope,$location,$http,$timeout) {
    //显示出弹框
    $scope.popShow = function (value) {
        $scope.ispopshow = value;
        $timeout(function () {
            $scope.ispopshow = false;
        },2000)
    }
    
    //动态跳转页面
    $scope.goPage = function (uri) {
        // alert(uri);
        $location.path(uri);
    }

    //设置title
    $scope.update_title = function(title) {
        var body = document.getElementsByTagName('body')[0];
        document.title = title;
        var iframe = document.createElement("iframe");
        iframe.setAttribute("src", "/empty.png");

        iframe.addEventListener('load', function() {
            setTimeout(function() {
                iframe.removeEventListener('load');
                document.body.removeChild(iframe);
            }, 0);
        });
        document.body.appendChild(iframe);
    }
}]);